package com.geeklan.smart.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.geeklan.smart.MLMApplication;
import com.geeklan.smart.data.model.User;
import com.geeklan.smart.data.model.UserInfo;

public class SharedPreferencesUtil {
    private static final String PREF_NAME = "sp_mlm";
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;

    // 初始化 SharedPreferences
    public static void init(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
        }
    }

    // 保存字符串
    public static void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    // 获取字符串
    public static String getString(String key, String defaultValue) {
        return sharedPreferences.getString(key, defaultValue);
    }

    // 保存整型
    public static void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.apply();
    }

    // 获取整型
    public static int getInt(String key, int defaultValue) {
        return sharedPreferences.getInt(key, defaultValue);
    }

    // 保存布尔值
    public static void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.apply();
    }

    // 获取布尔值
    public static boolean getBoolean(String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    // 保存浮点数
    public static void putFloat(String key, float value) {
        editor.putFloat(key, value);
        editor.apply();
    }

    // 获取浮点数
    public static float getFloat(String key, float defaultValue) {
        return sharedPreferences.getFloat(key, defaultValue);
    }

    // 保存长整型
    public static void putLong(String key, long value) {
        editor.putLong(key, value);
        editor.apply();
    }

    // 获取长整型
    public static long getLong(String key, long defaultValue) {
        return sharedPreferences.getLong(key, defaultValue);
    }

    // 删除数据
    public static void remove(String key) {
        editor.remove(key);
        editor.apply();
    }

    public static void putUser(UserInfo user){
        SharedPreferencesUtil.putString("birthday",user.getBirthday());
        SharedPreferencesUtil.putString("headImgUrl",user.getHeadImgUrl());
        SharedPreferencesUtil.putInt("sex",user.getSex());
        SharedPreferencesUtil.putString("nikename",user.getNikename());
        SharedPreferencesUtil.putInt("userId",user.getUserId());
        SharedPreferencesUtil.putString("username",user.getUsername());
        SharedPreferencesUtil.putBoolean("isLogin",true);
    }

    // 清空所有数据
    public static void clear() {
        String username = SharedPreferencesUtil.getString("username","");
        int userId = SharedPreferencesUtil.getInt("userId",0);
        MLMApplication.getUmeng().deleteAlias(String.valueOf(userId));
        MLMApplication.getUmeng().deleteTags();

        editor.clear();
        editor.commit();
        SharedPreferencesUtil.putString("username",username);
    }

    // 清空所有数据
    public static void clearAll() {
        int userId = SharedPreferencesUtil.getInt("userId",0);
        MLMApplication.getUmeng().deleteAlias(String.valueOf(userId));
        MLMApplication.getUmeng().deleteTags();

        editor.clear();
        editor.commit();
    }
}
